/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ee.ut.math.esi.service;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import ee.ut.math.esi.service.response.XML2SearchServiceResponse;
import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.xml.parsers.ParserConfigurationException;
import movierestsoapservice.BrowseServiceResponse;
import movierestsoapservice.SearchServiceResponse;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import org.xml.sax.SAXException;

/**
 * REST Web Service
 *
 * @author ool
 */
public class RottenTomatoesResource {
    
    private WebResource baseResource;
    private Client client;
    private static final String BASE_URL = "http://api.rottentomatoes.com/api/public/v1.0";
    private static final String API_KEY = "9afj9xnt3f8wghpz49hkwpkh";
    private static final String DEFAULT_PAGE_LIMIT = "20";

    /** Creates a new instance of RottenTomatoesResource */
    public RottenTomatoesResource() {
        ClientConfig config = new DefaultClientConfig();
        client = Client.create(config);
        baseResource = client.resource(BASE_URL);
    }

    /**
     * Retrieves representation of an instance of ee.ut.math.esi.RottenTomatoesResource
     * @return an instance of java.lang.String
     */
    public SearchServiceResponse search(String title, int page) throws JSONException, SAXException, IOException, ParserConfigurationException {
        WebResource resource = baseResource.path("movies.json");
        resource = resource.queryParam("q", title);
        resource = resource.queryParam("apikey", API_KEY);
        resource = resource.queryParam("page", page + "");
        resource = resource.queryParam("page_limit", DEFAULT_PAGE_LIMIT);
        
        String jsonResult = resource.accept(MediaType.APPLICATION_JSON).get(String.class);
        JSONObject json = new JSONObject("{ \"response\":" + jsonResult + "}");
        String xml = XML.toString(json);
        return XML2SearchServiceResponse.transformSearchServiceXml(xml);
    }
    
    public BrowseServiceResponse browse(String countryCode, int limit) throws JSONException, SAXException, IOException, ParserConfigurationException {
        WebResource resource = baseResource.path("lists/movies/box_office.json");
        resource = resource.queryParam("limit", limit + "");
        resource = resource.queryParam("apikey", API_KEY);
        resource = resource.queryParam("country", countryCode);
        
        String jsonResult = resource.accept(MediaType.APPLICATION_JSON).get(String.class);
        JSONObject json = new JSONObject("{ \"response\":" + jsonResult + "}");
        String xml = XML.toString(json);
        
        return XML2SearchServiceResponse.transformBrowseServiceXml(xml, limit);
    }
    
    public void close() {
        client.destroy();
    }
}
